﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ConsoleApp.Infrastructure.Attributes;
using ConsoleApp.Infrastructure.TaskFramework;
using ConsoleApp.Util;
using Xigua.MysqlExtensions.Attributes;
using Xigua.MysqlExtensions.Mapper;

namespace ConsoleApp.Job.TaskJob
{
	public class 数据测试任务 : TaskBase
	{
		static string reaCoon = "server=inner-adb-shusi.xiguaji.com;database=shusi_data;user id=shusi_online;password=d5UKrxwbq2ulOBf9YI*WdqUMl";
		private DapperHelper_MySql readDal = new DapperHelper_MySql(reaCoon);
		private DapperHelper_MySql writeDal = new DapperHelper_MySql(AppSettingsConstVars.WriteDbSqlConnection);
		public override void Execute()
		{
			//ParallerFor();
			ParallerForBreak();
			Console.ReadKey();


			int dateCode = 202301;
			try
			{
				for (int partid = 0; partid < 100; partid++)
				{
					HandlerPart(partid, dateCode);
				}
			}
			catch (Exception ex)
			{
				ConsoleHelper.WriteErrorLine($@"数据出现异常  {ex.ToString()}");
			}
			ConsoleHelper.WriteSuccessLine("结束");
			Console.ReadKey();
		}
		public void HandlerPart(int partid, int dateCode)
		{
			try
			{
				Stopwatch stopWatch = new Stopwatch();
				stopWatch.Start();

				var sql = $"select * from shusi_tmp.ProductInfoHalfYearStats_PartId where partid={partid} ";
				var b1lsit = readDal.Query<ProductInfoHalfYearStats_PartId>(sql);
				Console.WriteLine($"partid=={partid}");
				if (b1lsit != null && b1lsit.Count > 0)
				{
					var hololist = b1lsit.Select(e => new ProductInfoHalfYearStats_Writer()
					{
						Gids = e.Gids,
						PlatForm = e.PlatForm,
						DateMonth = e.DateMonth,
						BrandName = e.BrandName,
						MaxPrice = e.MaxPrice,
						MinPrice = e.MinPrice,
						Price = e.Price,
						SalesAmount = e.SalesAmount,
						SalesCount = e.SalesCount,
						AICateId1 = e.AICateId1,
						AICateId2 = e.AICateId2,
						AICateId3 = e.AICateId3,
						AICateId4 = e.AICateId4,
						AICateId5 = e.AICateId5,
						AICateIdLast = e.AICateIdLast,
						AICateName1 = e.AICateName1,
						AICateName2 = e.AICateName2,
						AICateName3 = e.AICateName3,
						AICateName4 = e.AICateName4,
						AICateName5 = e.AICateName5,
						AICateNameLast = e.AICateNameLast,
						BrandId = e.BrandId,
					}).ToList();
					hololist.SplitExecute(1000, items =>
					{
						try
						{
							writeDal.InsertOnDuplicateKeyUpdate(items);
							//ADBDataHelper_New.InsertOnDuplicateKeyUpdateSplitExcute(200, items, "shusi_tmp");
						}
						catch (Exception ex)
						{
							ConsoleHelper.WriteErrorLine(ex.ToString());
						}
					}, tryCount: 0);
				}
				ConsoleHelper.WriteSuccessLine($@"Done_{partid}");
				stopWatch.Stop();
				Console.WriteLine("耗时：" + stopWatch.ElapsedMilliseconds + " ms  ");
			}
			catch (Exception ex)
			{
				ConsoleHelper.WriteErrorLine(ex.ToString());
			}
		}

		public void ParallerFor()
		{
			ParallelLoopResult result = Parallel.For(0, 10, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, i =>
			{
				Console.WriteLine("迭代次数：{0},任务ID:{1},线程ID:{2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
				Thread.Sleep(10);
			});

			Console.WriteLine("是否完成:{0}", result.IsCompleted);
			Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
		}

		public void ParallerForBreak()
		{
			ParallelLoopResult result = Parallel.For(0, 10, (i, state) =>
			{
				Console.WriteLine("迭代次数：{0},任务ID:{1},线程ID:{2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
				Thread.Sleep(10);
				if (i >8)
					state.Break();
			});

			Console.WriteLine("是否完成:{0}", result.IsCompleted);
			Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
		}


		[MysqlTable(TableName = "ProductInfoHalfYearStats_PartId")]
		public class ProductInfoHalfYearStats_PartId
		{
			[MySqlColumn(KeyType = KeyType.Primary)]
			public string Gids { get; set; }
			[MySqlColumn(KeyType = KeyType.Primary)]
			public int PlatForm { get; set; }
			[MySqlColumn(KeyType = KeyType.Primary)]
			public int DateMonth { get; set; }
			public string BrandName { get; set; }
			public long MaxPrice { get; set; }
			public long MinPrice { get; set; }
			public long Price { get; set; }
			public long SalesAmount { get; set; }
			public long SalesCount { get; set; }
			public int AICateId1 { get; set; }
			public int AICateId2 { get; set; }
			public int AICateId3 { get; set; }
			public int AICateId4 { get; set; }
			public int AICateId5 { get; set; }
			public int AICateIdLast { get; set; }
			public string AICateName1 { get; set; }
			public string AICateName2 { get; set; }
			public string AICateName3 { get; set; }
			public string AICateName4 { get; set; }
			public string AICateName5 { get; set; }
			public string AICateNameLast { get; set; }
			public int BrandId { get; set; }
			public int PartId { get; set; }
		}

		[MysqlTable(TableName = "ProductInfoHalfYearStats")]
		public class ProductInfoHalfYearStats_Writer
		{
			[MySqlColumn(KeyType = KeyType.Primary)]
			public string Gids { get; set; }
			[MySqlColumn(KeyType = KeyType.Primary)]
			public int PlatForm { get; set; }
			[MySqlColumn(KeyType = KeyType.Primary)]
			public int DateMonth { get; set; }
			public string BrandName { get; set; }
			public long MaxPrice { get; set; }
			public long MinPrice { get; set; }
			public long Price { get; set; }
			public long SalesAmount { get; set; }
			public long SalesCount { get; set; }
			public string Title { get; set; }
			public int AICateId1 { get; set; }
			public int AICateId2 { get; set; }
			public int AICateId3 { get; set; }
			public int AICateId4 { get; set; }
			public int AICateId5 { get; set; }
			public int AICateIdLast { get; set; }
			public string AICateName1 { get; set; }
			public string AICateName2 { get; set; }
			public string AICateName3 { get; set; }
			public string AICateName4 { get; set; }
			public string AICateName5 { get; set; }
			public string AICateNameLast { get; set; }
			public int BrandId { get; set; }
		}
	}
}




